#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#     Filename @  getQSBK.py
#       Author @  Huoty
#  Create date @  2015-06-25 10:08:02
#  Description @  获取糗百段子
# *************************************************************

import os
import sys
import getopt
import time
import urllib
import urllib2
import re

qsbk_page_uri = "http://www.qiushibaike.com/8hr/page/"
user_agent = "Mozilla/5.0 (X11; Linux i686)"

class QSBK(object):
    '''获取糗百不带图片的段子'''

    def __init__(self, page_uri, user_agent):
        '''初始化函数'''
        self.page_uri = page_uri
        self.headers = {"User-Agent": user_agent}
        self.reg = '<div class="article block untagged mb15".*?>.*?<div class="author">.*?<a.*?>.*?<img.*?>(.*?)</a>.*?<div class="content">(.*?)<!--(.*?)-->.*?</div>(.*?)<div class="stats">.*?<i class="number">(.*?)</i>'

        self.page = 0 # 当前读取的页面数
        self.stories = [] # 安页存放段子
        self.enable = False # 控制程序运行

    def getPageCode(self, page):
        '''获取特定页面的代码'''
        url = self.page_uri + str(page)
        try:
            request = urllib2.Request(url, headers = self.headers)
            response = urllib2.urlopen(request)
            #print response.read()
            content = response.read().decode("utf-8")
            return content

        except urllib2.URLError, e:
            print "页面读取失败！",
            if hasattr(e, "reason"):
                print "失败原因:", e.reason
            return None

    def getPageStories(self, page_code):
        '''通过页面代码提取段子，包含段子作者、内容、点赞数以及发布时间'''
        pageStories = [] # 存放每一页中提取到的段子信息
        pattern = re.compile(self.reg, re.S)
        items = re.findall(pattern, page_code)

        for item in items:
            haveMedia = re.search("img|video", item[3])
            if not haveMedia: # 没有图片的视频时提取相应段子
                # item[1].strip().replace('<br/>','\n')
                strinfo = re.compile('<br.*?/>')  # 将html换行标签替换成换行符
                content = strinfo.sub('\n', item[1].strip())
                pageStories.append([item[0].strip(), item[2].strip(), item[4].strip(), content])
                # 分别对应 1.发布者 2.发布时间 3.点赞数 4.段子内容

        return pageStories

    def loadPage(self):
        '''新加载一页的内容到全局变量中'''
        page_code = self.getPageCode(self.page + 1)

        if page_code:
            page_stories = self.getPageStories(page_code)

        if page_stories:
            self.stories.append(page_stories)

    def OutputOneStory(self, istory):
        '''想屏幕输出一个段子'''
        os.system('clear')
        print u"\n发布作者:%s\t发布时间:%s\t点赞数:%s\t第 %s 页" % (istory[0], istory[1], istory[2], self.page)
        print u"-" * 80
        print u"\n%s\n" % istory[3]
        print u"--------------- 本软件由 huoty 开发   版权所有    2015.06.26 ---------------"

    def startup(self, auto = 0, interval = 10):
        '''启动程序，可设置自动下一条'''
        os.system('clear')
        print u'欢迎使用糗百爬虫程序\n\n作者：huoty\n日期：2015-06-26\n版本：1.0\n\n回车查看和刷新段子，quit 退出',
        input = raw_input()
        if input and (input[0] == "q" or input[0] == "Q"):
            return

        os.system('clear')
        sys.stdout.write("\n正在加载内容...")
        sys.stdout.flush()
        self.enable = True
        self.loadPage()
        while self.enable:
            self.page += 1
            if len(self.stories) < 2:
                self.loadPage()

            pageStories = self.stories[0]
            del self.stories[0]
            for story in pageStories:
                self.OutputOneStory(story)  # 输出一条段子

                if auto == 0:
                    print u"\n回车刷新段子，quit 退出",
                    input = raw_input()  # 等待用户输入
                    if input and (input[0] == "q" or input[0] == "Q"):
                        self.enable = False
                        break
                else:
                    time.sleep(interval)

        # 刷新控制台
        os.system('clear')

def usage():
    print u'''
Usage: qiusbk command

qiusbk is a simple command line for getting the stories of qiushibaike.

Commands:
    h | --help        Show usage
    a | --auto        Automatic load
'''

# Script starts from here

if __name__ == "__main__":
    try:
        opts, args = getopt.getopt(sys.argv[1:], "ha:", ["help", "auto="])
    except getopt.GetoptError, e:
        # print help information and exit:
        print "error:", e
        usage()
        sys.exit()

    spider = QSBK(qsbk_page_uri, user_agent)
    if opts:
        for o, a in opts:
            if o in ("-h", "--help"):
                usage()
                sys.exit()
            if o in ("-a", "--auto"):
                spider.startup(1, int(a))
    else:
        spider.startup()
